package retrofitdemo.com.night.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Stack;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import retrofitdemo.com.night.R;

/**
 * Created by 86178 on 2020/3/12.
 * author mogen
 * 用心，成就一切。
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //锁定竖屏
        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setStatusBar( R.color.white);

        mI_fragmentGroupLayoutId = getI_fragmentGroupLayoutId();
    }


    //标题栏返回调用方法
    public void setBack() {
        findViewById(R.id.topBarRelative).setVisibility(View.VISIBLE);
        findViewById(R.id.leftTitleText).setVisibility(View.GONE);
        findViewById(R.id.topBarRelative).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void setLeftVis(){
        findViewById(R.id.topBarRelative).setVisibility(View.GONE);
    }

    public void showToast(String s){
        Toast.makeText(BaseActivity.this,s,Toast.LENGTH_SHORT).show();
    }

    //设置标题栏中心标题
    public void setTitleText(String str) {
        try {
            TextView text = findViewById(R.id.bar_title);
            text.setText(str);
        } catch (Exception e) {
        }
    }

    //设置标题栏右侧字体（如果是文字）
    public void setRightText(String str) {
        TextView text = findViewById(R.id.rightText);
        text.setText(str);
        text.setOnClickListener(this);
    }

    //设置标题栏右侧字体颜色（如果是文字）
    public void setRightTextColor(int id) {
        TextView text = findViewById(R.id.rightText);
        text.setTextColor(this.getResources().getColor(id));
    }

    //设置标题栏右侧图片
    public void setRightImg(int id) {
        ImageView image = findViewById(R.id.rightImage);
        image.setImageResource(id);
        image.setOnClickListener(this);
    }

    //设置标题栏左侧字体（如果是文字）
    public void setLeftText(String str) {
        findViewById(R.id.topBarRelative).setVisibility(View.VISIBLE);
        findViewById(R.id.left_icon).setVisibility(View.GONE);
        TextView text = findViewById(R.id.leftTitleText);
        text.setText(str);
        text.setOnClickListener(this);
    }

    public void setTitleBg(int id) {
        RelativeLayout layout = findViewById(R.id.view_base_title_bar);
        layout.setBackgroundColor(id);
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * Android 6.0 以上设置状态栏颜色
     */
    protected void setStatusBar(@ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 设置状态栏底色颜色
            getWindow().addFlags( WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(color);
            // 如果亮色，设置状态栏文字为黑色
            if (isLightColor(color)) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }

    }

    /**
     * 判断颜色是不是亮色
     *
     * @param color
     * @return
     * @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android
     */
    private boolean isLightColor(@ColorInt int color) {
        return ColorUtils.calculateLuminance(color) >= 0.5;
    }

    /**
     * 获取StatusBar颜色，默认白色
     *
     * @return
     */
    protected @ColorInt int getStatusBarColor() {
        return Color.WHITE;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private SparseArray<Fragment> mSArray_fragment = new SparseArray<>();
    public void showFragment(Fragment fragment, int tag) {
        if (fragment == null) {
            throw new NullPointerException("fragment :" + null + " NullPointerException");
        }
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction sTransaction = mFragmentManager.beginTransaction();
        boolean sB_exist = false;
        if (mSArray_fragment.size() > 0) {
            for (int i = 0; i < mSArray_fragment.size(); i++) {
                Fragment sFragment = mSArray_fragment.valueAt(i);
                if (sFragment.getClass().getSimpleName().equals(fragment.getClass().getSimpleName())) {
                    sB_exist = true;
                    continue;
                }
                sTransaction.hide(sFragment);
            }
        }

        if (!sB_exist) {
            sTransaction.add(getI_fragmentGroupLayoutId() == 0 ? mI_fragmentGroupLayoutId : getI_fragmentGroupLayoutId(), fragment);
        } else {
            sTransaction.show(fragment);
        }
        sTransaction.commit();
        mSArray_fragment.put(tag, fragment);
    }

    private int mI_fragmentGroupLayoutId;
    public int getI_fragmentGroupLayoutId() {
        return 0;
    }

}
